﻿using Coolzon.UI.Core.Runtime.Script;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Coolzon.UI.Core.Parts.Widgets
{
    public class Label : Widget
    {
        System.Windows.Controls.TextBlock _textBlock;
        System.Windows.Controls.Image _image;

        #region XProperties-Fields
        private TextAlignment _textAlignment = TextAlignment.Left;
        private string _imageString;
        #endregion

        #region XProperties

        [Category("Appearance")]
        [DisplayName("文字对其模式")]
        [Description("TextAlignment")]
        //[Editor(typeof(XProperty.Editors.XImageSourceFileEditor), typeof(XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Appearance, "Source", "图片地址", false, false)]
        public string Image
        {
            get { return _imageString; }
            set
            {
                if (string.Equals(_imageString, value) == false)
                {
                    _imageString = value;
                    _image.Source = Coolzon.UI.Core.SolutionResouseManager.GetImage(_imageString);
                }
            }
        }

        [Category("Appearance")]
        [DisplayName("文字对其模式")]
        [Description("TextAlignment")]
        //[Editor(typeof(XProperty.Editors.XImageSourceFileEditor), typeof(XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Appearance, "TextAlignment", "文字对其模式", false, false)]
        public TextAlignment TextAlignment
        {
            get { return _textAlignment; }
            set { _textAlignment = value; }
        }

        [Category("Appearance")]
        [DisplayName("文字对其模式")]
        [Description("TextAlignment")]
        //[Editor(typeof(XProperty.Editors.XImageSourceFileEditor), typeof(XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Appearance, "Text", "文字", false, false)]
        public string Text
        {
            get
            {
                return _textBlock.Text;
            }
            set
            {
                _textBlock.Text = value;
            }
        }
        
        #endregion
        
        public Label()
        {
            this.FontSize = 9;
            _textBlock = new System.Windows.Controls.TextBlock();
            _image = new System.Windows.Controls.Image();
            Grid grid = new Grid();
            ColumnDefinition c0 = new ColumnDefinition();
            ColumnDefinition c1 = new ColumnDefinition();
            c0.Width = new GridLength(0, GridUnitType.Auto);
            c1.Width = new GridLength(1, GridUnitType.Star);
            grid.ColumnDefinitions.Add(c0);
            grid.ColumnDefinitions.Add(c1);
            grid.Children.Add(_image);
            grid.Children.Add(_textBlock);
            Grid.SetColumn(_image, 0);
            Grid.SetColumn(_textBlock, 1);
            //_textBlock.Background = Brushes.Yellow;
            this.Content = grid;
        }

        protected override void OnInit(PartContext context)
        {
            _textBlock.Foreground = this.Foreground;
            _textBlock.FontSize = FontSize;
            _textBlock.TextAlignment =TextAlignment;
            _textBlock.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
            _textBlock.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            base.OnInit(context);
        }

        protected override object OnCreateScriptObject()
        {
            return new LabelScriptObject(this);
        }
    }
}
